/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trash;

/**
 *
 * @author ain
 */
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.security.auth.login.AppConfigurationEntry;
import javax.security.auth.login.Configuration;
import javax.security.auth.login.AppConfigurationEntry.LoginModuleControlFlag;

public class LiteConfiguration
        extends Configuration {

    static private LiteConfiguration dbConfig;

    static public void init() {
        dbConfig = new LiteConfiguration();
        Configuration.setConfiguration(dbConfig);
    }

    static public LiteConfiguration getLiteConfiguration() {
        return dbConfig;
    }

    public void addAppConfigurationEntry(String appName,
            AppConfigurationEntry entry) {
    }

    public boolean deleteAllAppEntries(String appName) {
        return true;
    }

    public boolean deleteAppConfigurationEntry(String appName,
            String loginModuleName) {
        return true;
    }

    public AppConfigurationEntry[] getAppConfigurationEntry(
            String applicationName) {
        List entries = new ArrayList();
        String loginModuleClass = "unc.pds.auth.PDSLoginModule";
        String controlFlagValue = "OPTIONAL";
        AppConfigurationEntry.LoginModuleControlFlag controlFlag =
                resolveControlFlag(controlFlagValue);
        AppConfigurationEntry entry = new AppConfigurationEntry(
                loginModuleClass, controlFlag, new HashMap());
        entries.add(entry);
        return (AppConfigurationEntry[]) entries.toArray(new AppConfigurationEntry[entries.size()]);
    }

    public void refresh() {
    }

    static String controlFlagString(LoginModuleControlFlag flag) {
        if (LoginModuleControlFlag.REQUIRED.equals(flag)) {
            return "REQUIRED";
        } else if (LoginModuleControlFlag.REQUISITE.equals(flag)) {
            return "REQUISITE";
        } else if (LoginModuleControlFlag.SUFFICIENT.equals(flag)) {
            return "SUFFICIENT";
        } else if (LoginModuleControlFlag.OPTIONAL.equals(flag)) {
            return "OPTIONAL";
        } else {
            LOGGER.logp(
                    Level.WARNING,
                    CLASS_NAME,
                    "resolveControlFlag",
                    "{0} is an unknown LoginModuleControlFlag. Returning LoginModuleControlFlag.OPTIONAL",
                    flag);
            return "OPTIONAL";
        }

    }

    static LoginModuleControlFlag resolveControlFlag(String name) {
        if (name == null) {
            throw new NullPointerException(
                    "control flag name passed in is null.");
        }

        String uppedName = name.toUpperCase(Locale.US);
        if ("REQUIRED".equals(uppedName)) {
            return LoginModuleControlFlag.REQUIRED;
        } else if ("REQUISITE".equals(uppedName)) {
            return LoginModuleControlFlag.REQUISITE;
        } else if ("SUFFICIENT".equals(uppedName)) {
            return LoginModuleControlFlag.SUFFICIENT;
        } else if ("OPTIONAL".equals(uppedName)) {
            return LoginModuleControlFlag.OPTIONAL;
        } else {
            LOGGER.logp(
                    Level.WARNING,
                    CLASS_NAME,
                    "resolveControlFlag",
                    "{0} (up-cased) is an unknown String controlFlag. Returning OPTIONAL",
                    uppedName);
            return AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL;
        }
    }
    static private String CLASS_NAME = LiteConfiguration.class.getName();
    static private Logger LOGGER = Logger.getLogger(CLASS_NAME);
}
